package com.astrofizzbizz.emailtextfile;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.mail.Message.RecipientType;

import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;
import org.codemonkey.simplejavamail.TransportStrategy;

public class EmailTextFile extends Email
{
	public EmailTextFile()
	{
		super();
	}
	public void addTextFileAttachment(String filePath, String attachmentName) throws IOException
	{
	    File file = new File(filePath);
    	FileInputStream fin = new FileInputStream(file);
    	byte[] fileContent = new byte[(int)file.length()];
    	fin.read(fileContent);
    	fin.close();
    	addAttachment(attachmentName, fileContent, "plain/txt");
	}
	public void addTextFileAttachment(String attachmentName, byte[] textByteArray)
	{
		super.addAttachment(attachmentName, textByteArray, "plain/txt");

	}
	public class EmailTextFileException extends Exception
	{
		private static final long serialVersionUID = 5917219265841199016L;
		String message;

		public EmailTextFileException(String message)
		{
			super();
			this.message = message;
		}
		public String getMessage()
		{
			return message;
		}
	}
	public void setFromAddress(String name, String fromAddress)
	{
		super.setFromAddress(name, fromAddress);
	}
	public void setSubject(String subject)
	{
		super.setSubject(subject);
	}
	public void addRecipient(String name, String address)
	{
		super.addRecipient(name, address, RecipientType.TO);
	}
	public void setText(String text)
	{
		super.setText(text);
	}
	public void sendGmail(String gmailUser, String password) throws EmailTextFileException 
	{
		try
		{
			new Mailer("smtp.gmail.com", 465, gmailUser, password, TransportStrategy.SMTP_SSL).sendMail(this);
		}
		catch(org.codemonkey.simplejavamail.MailException e)
		{
			throw new EmailTextFileException("Could not send mail");
		}
	}
	public static void main(String[] args) throws IOException 
	{
		
		final EmailTextFile email = new EmailTextFile();

		email.setFromAddress("David McGinnis", "dmcginnis427@gmail.com");
		email.setSubject("Transmission Line Again");
		email.addRecipient("David McGinnis", "david.mcginnis@esss.se", RecipientType.TO);
		email.setText("Hej Dave, The Impedance is 5 ohms");
		email.addTextFileAttachment("C:\\Dropbox\\McGinnisFiles\\EclipseWorkspace\\AstroFizzBizz\\StoneEdgeKey", "test.txt");
		try {
			email.sendGmail("dmcginnis427@gmail.com", "crap");
		} catch (EmailTextFileException e) {
			System.out.println(e.getMessage());
		}

	}

}
